﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;
using System.Web.Caching;
using Avatar.MembershipService;

public partial class Admin_AccessRole : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetFileList();
        }
    }
    private void GetFileList()
    {
        string _rootPath = Server.MapPath("~/");

        TreeNode _rootNode = new TreeNode("网站系统");
        _rootNode.SelectAction = TreeNodeSelectAction.None;
        TreeView1.Nodes.Add(_rootNode);
        TreeView1.ExpandDepth = 1;
        //TreeView1.TreeNodePopulate += new TreeNodeEventHandler(NodePopulate);

        if (Cache["RootNode"] == null)
        {
            Response.Write("NotUseCache!");
            DirectoryInfo _dirInfo = new DirectoryInfo(_rootPath);
            if (_dirInfo.Exists)
            {
                List<TreeNode> _nList = new List<TreeNode>();

                foreach (var _fileSystemInfo in _dirInfo.GetFileSystemInfos())
                {
                    TreeNode _node = new TreeNode();

                    if (_fileSystemInfo is FileInfo)
                    {
                        if (_fileSystemInfo.Extension.ToLower() == ".aspx")
                        {
                            _node.Text = _fileSystemInfo.Name;
                            _node.Value = "~/" + _node.Text;
                            _node.NavigateUrl = _node.Value;
                            _node.Target = "_blank";
                            _node.ShowCheckBox = true;

                            _nList.Add(_node);
                        }
                    }
                    else
                    {
                        var _directoryInfo = (DirectoryInfo)_fileSystemInfo;
                        _node.Text = _directoryInfo.Name;
                        _node.PopulateOnDemand = true;
                        _node.SelectAction = TreeNodeSelectAction.Expand;
                        _node.ShowCheckBox = false;

                        int _fileCount = 0;
                        DirectoryInfo _dir = new DirectoryInfo(Server.MapPath("../"+_directoryInfo.Name));
                        if (_dir.Exists)
                        {
                            _fileCount = _dir.GetFiles().Count(f => f.Extension.ToLower() == ".aspx");
                        } 
                        if (_fileCount > 0)
                        {
                            _nList.Add(_node);
                        }
                    }
                }
                Cache.Insert("RootNode", _nList);
            }
        }
        List<TreeNode> _nodeList = (List<TreeNode>)Cache["RootNode"];
        foreach (var _n in _nodeList)
        {
            _rootNode.ChildNodes.Add(_n);
        }

        TreeView1.DataBind();
    }
    protected void NodePopulate(object sender, TreeNodeEventArgs e)
    {
        //if (Cache[e.Node.Text] != null)
        //{
        ArrayList _files = new ArrayList();
        DirectoryInfo _dir = new DirectoryInfo(Server.MapPath("../" + e.Node.Text));
        if (_dir.Exists)
        {
            foreach (var _f in _dir.GetFileSystemInfos())
            {
                if (_f.Extension.ToLower() == ".aspx")
                {
                    _files.Add(_f.Name);
                }
            }
        }
        //Cache.Insert(e.Node.Text, _files);
        //}
        //ArrayList _fileList = (ArrayList)Cache[e.Node.Text];

        foreach (var _f in _files)
        {
            TreeNode _node = new TreeNode(_f.ToString());
            _node.Value = "~/" + e.Node.Text + "/" + _f.ToString();
            _node.NavigateUrl = _node.Value;
            _node.Target = "_blank";
            _node.ShowCheckBox = true;

            e.Node.ChildNodes.Add(_node);
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            int _groupID = Convert.ToInt32(list_Group.SelectedValue);
            bool _access = Convert.ToBoolean(list_Access.SelectedValue);

            foreach (TreeNode _n in TreeView1.CheckedNodes)
            {
                AccessRoleManager.AddRole(_groupID, _n.Value, _access);
            }

            Response.Redirect("AccessRole.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "用户访问规则添加失败:" + _e.Message;
        }
    }
}
